package ml.kgc.dao.impl;

import ml.kgc.dao.Base;
import ml.kgc.dao.NewsDao;
import ml.kgc.pojo.News;
import ml.kgc.pojo.NewsCategory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl extends Base implements NewsDao{

    //获取新闻总数量
    public int getTotalCount(){
        int total = 0;//新闻总数量
        String sql = "select count(1) from news_detail";
        Object[] params = {};
        ResultSet rs = this.executeSQL(sql,params);
        try {
            while (rs.next()){
                total = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return total;
    }
    //分页获取新闻数据  pageNo:当前页码，pageSize：页面容量
    public List<News> getPageNewsList(int pageNo,int pageSize){
        List<News> list = new ArrayList<News>();
        String sql = "select id,title,author,createdate from news_detail where 1=1 order by createdate desc limit ?,?";
        Object[] params = {(pageNo-1)*pageSize,pageSize};
        ResultSet rs = this.executeSQL(sql, params);
        try {
        while (rs.next()){
            int id = rs.getInt("id");
            String title = rs.getString("title");
            String author = rs.getString("author");
            Timestamp createdate = rs.getTimestamp("createdate");
            News news = new News();
            news.setTitle(title);
            news.setAuthor(author);
            news.setCreateDate(createdate);
            list.add(news);
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
        return list;
    }
    public News getNewsById(int id){
        News news = new News();
        try {
            String sql = "select title,summary,author,content,createdate,picpath from news_detail where id=?";
            Object[] params = {id};
            ResultSet rs = this.executeSQL(sql, params);
            while (rs.next()){
                String title = rs.getString("title");
                String summary = rs.getString("summary");
                String author = rs.getString("author");
                String content = rs.getString("content");
                Timestamp createdate = rs.getTimestamp("createdate");
                String picpath = rs.getString("picpath");

                news.setTitle(title);
                news.setSummary(summary);
                news.setAuthor(author);
                news.setContent(content);
                news.setCreateDate(createdate);
                news.setPicPath(picpath);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return news;
    }

    public List<News> getNewsList() {
        List<News> list = new ArrayList<News>();
        try {
            String sql = "select id,categoryId,title,summary,content,author,createDate from news_detail";
            Object[] params = {};
            ResultSet rs = this.executeSQL(sql,params);
            while(rs.next()){
                int id = rs.getInt("id");
                int categoryId = rs.getInt("categoryId");
                String newTitle = rs.getString("title");
                String summary = rs.getString("summary");
                String content = rs.getString("content");
                String author = rs.getString("author");
                Timestamp createdate = rs.getTimestamp("createdate");
                //System.out.println(id+"--"+categoryId+"--"+newTitle+"--"+summary+"--"+content+"--"+author+"--"+createdate);
                News news = new News();
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(newTitle);
                news.setSummary(summary);
                news.setContent(content);
                news.setAuthor(author);
                news.setCreateDate(createdate);
                list.add(news);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeResource();
        }
        return list;
    }

    //增加新闻信息
    //public void addNews(int id, int categoryId, String title, String summary, String content, String author, Date createDate) {
    public boolean addNews(News news) {
        boolean flag = false;
        try {
            String sql = "insert into news_detail(categoryId,title,summary,content,author,createDate,picpath) VALUES(?,?,?,?,?,?,?)";
            //Object[] params = {id, categoryId, title, summary, content, author, new Timestamp(createDate.getTime())};
            Object[] params = {news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getAuthor(), new Timestamp(news.getCreateDate().getTime()),news.getPicPath()};
            int i = this.executeUpdate(sql,params);
            if (i > 0) {
                flag = true;
                System.out.println("插入新闻成功！");
            }
        }finally {
            this.closeResource();
        }
        return flag;
    }

    //public void deleteNews(int id) {
    public void deleteNews(News news) {
        try {
            String sql = "delete from news_detail where id=?";
            //Object[] params = {id};
            Object[] params = {news.getId()};
            int i = this.executeUpdate(sql, params);
            if (i > 0) {
                System.out.println("删除新闻成功！");
            }
        }finally {
            this.closeResource();
        }
    }


    //public void update(int id, String title) {
    public void update(News news) {
        try {
            String sql = "update news_detail set title=? where id=?";
            //Object[] params = {title,id};
            Object[] params = {news.getTitle(),news.getId()};
            int i = this.executeUpdate(sql, params);
            if (i > 0) {
                System.out.println("修改新闻成功！");
            }
        }finally {
            this.closeResource();
        }
    }


    //public void getNewsByTitle(String title) {
    public void getNewsByTitle(News news) {
        try {
            String sql = "select id,title from news_detail where title like?";
            //Object[] params = {title};
            Object[] params = {news.getTitle()};
            ResultSet rs = this.executeSQL(sql, params);
            while (rs.next()){
                int id = rs.getInt("id");
                String newsTitle = rs.getString("title");
                System.out.println(id+"--"+newsTitle);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeResource();
        }
    }

    //查询某个新闻分类下是否有新闻
    public int getCountByCateory(NewsCategory newsCategory) {
        int count = 0;
        String sql = "select count(1) from news_detail where categoryId = ?";
        Object[] params = {newsCategory.getId()};
        ResultSet rs = this.executeSQL(sql,params);
        try {
            while (rs.next()){
                count = rs.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return count;
    }



    public static void main(String[] args) {
        NewsDao newsDao = new NewsDaoImpl();

        //修改为封装到News类
        News news = new News();
        /*news.setId(5);
        news.setCategoryId(2);
        news.setTitle("test");
        news.setSummary("test");
        news.setContent("test");
        news.setAuthor("admin");
        news.setCreateDate(new Date());
        newsDao.addNews(news);*/

        news.setId(5);
        news.setTitle("kkkk");
        newsDao.update(news);

		//newsDao.addNews(3, 2, "test","test", "test","马腾龙",new Date());
		//newsDao.update(3,"课工场test");
        //newsDao.deleteNews(4);
        newsDao.getNewsList();
		//newsDao.getNewsByTitle("%课工场%");



    }




}

